

************************
** Study 2: Figure S2.18
************************

* Package
*ssc install cem, replace



** 2017 Results
***************

* Covariates macro
global covars = "gender agecat edu pol_interest lr vote2015 refvote2016 region"
global covars_cem = "gender(#0) agecat(#0) edu(#0) pol_interest(#0) lr(#0) vote2015(#0) refvote2016(#0) region(#0)"

* Open dataset
use "LondonBridgeAttack2017.dta", replace

* Drop incomplete entries
egen rowmiss = rowmiss($covars)
keep if rowmiss == 0

* 1 day
cem $covars_cem if statadate == td(02jun2017) & sec != . | statadate == td(04jun2017) & sec != ., treatment(postattack)
rename cem_weights cem_weights1sec
cem $covars_cem if statadate == td(02jun2017) & imm != . | statadate == td(04jun2017) & imm != ., treatment(postattack)
rename cem_weights cem_weights1imm
cem $covars_cem if statadate == td(02jun2017) & britid != . | statadate == td(04jun2017) & britid != ., treatment(postattack)
rename cem_weights cem_weights1britid
cem $covars_cem if statadate == td(02jun2017) & englid != . | statadate == td(04jun2017) & englid != ., treatment(postattack)
rename cem_weights cem_weights1englid
reg sec postattack [iweight = cem_weights1sec]
estimates store sec1
reg imm postattack [iweight = cem_weights1imm]
estimates store imm1
reg britid postattack [iweight = cem_weights1britid]
estimates store britid1
reg englid postattack [iweight = cem_weights1englid]
estimates store englid1

* 2 days
cem $covars_cem if inrange(statadate,td(01jun2017),td(02jun2017)) & sec != . | inrange(statadate,td(04jun2017),td(05jun2017)) & sec != ., treatment(postattack)
rename cem_weights cem_weights2sec
cem $covars_cem if inrange(statadate,td(01jun2017),td(02jun2017)) & imm != . | inrange(statadate,td(04jun2017),td(05jun2017)) & imm != ., treatment(postattack)
rename cem_weights cem_weights2imm
cem $covars_cem if inrange(statadate,td(01jun2017),td(02jun2017)) & britid != . | inrange(statadate,td(04jun2017),td(05jun2017)) & britid != ., treatment(postattack)
rename cem_weights cem_weights2britid
cem $covars_cem if inrange(statadate,td(01jun2017),td(02jun2017)) & englid != . | inrange(statadate,td(04jun2017),td(05jun2017)) & englid != ., treatment(postattack)
rename cem_weights cem_weights2englid
reg sec postattack [iweight = cem_weights2sec]
estimates store sec2
reg imm postattack [iweight = cem_weights2imm]
estimates store imm2
reg britid postattack [iweight = cem_weights2britid]
estimates store britid2
reg englid postattack [iweight = cem_weights2englid]
estimates store englid2

* 3 days
cem $covars_cem if inrange(statadate,td(31may2017),td(02jun2017)) & sec != . | inrange(statadate,td(04jun2017),td(06jun2017)) & sec != ., treatment(postattack)
rename cem_weights cem_weights3sec
cem $covars_cem if inrange(statadate,td(31may2017),td(02jun2017)) & imm != . | inrange(statadate,td(04jun2017),td(06jun2017)) & imm != ., treatment(postattack)
rename cem_weights cem_weights3imm
cem $covars_cem if inrange(statadate,td(31may2017),td(02jun2017)) & britid != . | inrange(statadate,td(04jun2017),td(06jun2017)) & britid != ., treatment(postattack)
rename cem_weights cem_weights3britid
cem $covars_cem if inrange(statadate,td(31may2017),td(02jun2017)) & englid != . | inrange(statadate,td(04jun2017),td(06jun2017)) & englid != ., treatment(postattack)
rename cem_weights cem_weights3englid
reg sec postattack [iweight = cem_weights3sec]
estimates store sec3
reg imm postattack [iweight = cem_weights3imm]
estimates store imm3
reg britid postattack [iweight = cem_weights3britid]
estimates store britid3
reg englid postattack [iweight = cem_weights3englid]
estimates store englid3

* Results table including Cohen's d estimates
gen days = _n in 1/3
foreach var of varlist sec imm britid englid {
gen n_`var' = .
gen pe_`var' = .
gen se_`var' = .
gen p_`var' = .
gen ll_`var' = .
gen ul_`var' = .
gen sd_`var' = .
gen d_`var' = .
gen dlow_`var' = .
gen dup_`var' = .
forvalues i = 1/3 {
estimates restore `var'`i'
replace n_`var' = `e(N)' if days == `i'
estimates replay `var'`i'
matrix R = r(table)
replace pe_`var' = R[1,1] if days == `i'
replace se_`var' = R[2,1] if days == `i'
replace p_`var' = R[4,1] if days == `i'
replace ll_`var' = R[5,1] if days == `i'
replace ul_`var' = R[6,1] if days == `i'
sum `var' [iweight = cem_weights`i'`var']
replace sd_`var' = `r(sd)' if days == `i'
replace d_`var' = pe_`var'/sd_`var' if days == `i'
replace dlow_`var' = pe_`var'/sd_`var' - 1.96*se_`var'/sd_`var' if days == `i'
replace dup_`var' = pe_`var'/sd_`var' + 1.96*se_`var'/sd_`var' if days == `i'
}
}

drop newid - cem_weights3englid
drop _est*
keep in 1/3
gen year = 2017
order year

save "Exact2017.dta", replace




** 2019 Results
***************

* Covariates macro
global covars = "gender agecat edu pol_interest lr vote2017 refvote2016 region"
global covars_cem = "gender(#0) agecat(#0) edu(#0) pol_interest(#0) lr(#0) vote2017(#0) refvote2016(#0) region(#0)"

* Open dataset
use "LondonBridgeAttack2019.dta", replace

* Drop incomplete entries
egen rowmiss = rowmiss($covars)
keep if rowmiss == 0

* 1 day
cem $covars_cem if statadate == td(28nov2019) & sec != . | statadate == td(30nov2019)& sec != ., treatment(postattack)
rename cem_weights cem_weights1sec
cem $covars_cem if statadate == td(28nov2019) & imm != . | statadate == td(30nov2019) & imm != ., treatment(postattack)
rename cem_weights cem_weights1imm
cem $covars_cem if statadate == td(28nov2019) & britid != . | statadate == td(30nov2019) & britid != ., treatment(postattack)
rename cem_weights cem_weights1britid
cem $covars_cem if statadate == td(28nov2019) & englid != . | statadate == td(30nov2019) & englid != ., treatment(postattack)
rename cem_weights cem_weights1englid
reg sec postattack [iweight = cem_weights1sec]
estimates store sec1
reg imm postattack [iweight = cem_weights1imm]
estimates store imm1
reg britid postattack [iweight = cem_weights1britid]
estimates store britid1
reg englid postattack [iweight = cem_weights1englid]
estimates store englid1
	
* 2 days
cem $covars_cem if inrange(statadate,td(27nov2019),td(28nov2019)) & sec != . | inrange(statadate,td(30nov2019),td(1dec2019)) & sec != ., treatment(postattack)
rename cem_weights cem_weights2sec
cem $covars_cem if inrange(statadate,td(27nov2019),td(28nov2019)) & imm != . | inrange(statadate,td(30nov2019),td(1dec2019)) & imm != ., treatment(postattack)
rename cem_weights cem_weights2imm
cem $covars_cem if inrange(statadate,td(27nov2019),td(28nov2019)) & britid != . | inrange(statadate,td(30nov2019),td(1dec2019)) & britid != ., treatment(postattack)
rename cem_weights cem_weights2britid
cem $covars_cem if inrange(statadate,td(27nov2019),td(28nov2019)) & englid != . | inrange(statadate,td(30nov2019),td(1dec2019)) & englid != ., treatment(postattack)
rename cem_weights cem_weights2englid
reg sec postattack [iweight = cem_weights2sec]
estimates store sec2
reg imm postattack [iweight = cem_weights2imm]
estimates store imm2
reg britid postattack [iweight = cem_weights2britid]
estimates store britid2
reg englid postattack [iweight = cem_weights2englid]
estimates store englid2

* 3 days
cem $covars_cem if inrange(statadate,td(26nov2019),td(28nov2019)) & sec != . | inrange(statadate,td(30nov2019),td(2dec2019)) & sec != ., treatment(postattack)
rename cem_weights cem_weights3sec
cem $covars_cem if inrange(statadate,td(26nov2019),td(28nov2019)) & imm != . | inrange(statadate,td(30nov2019),td(2dec2019)) & imm != ., treatment(postattack)
rename cem_weights cem_weights3imm
cem $covars_cem if inrange(statadate,td(26nov2019),td(28nov2019)) & britid != . | inrange(statadate,td(30nov2019),td(2dec2019)) & britid != ., treatment(postattack)
rename cem_weights cem_weights3britid
cem $covars_cem if inrange(statadate,td(26nov2019),td(28nov2019)) & englid != . | inrange(statadate,td(30nov2019),td(2dec2019)) & englid != ., treatment(postattack)
rename cem_weights cem_weights3englid
reg sec postattack [iweight = cem_weights3sec]
estimates store sec3
reg imm postattack [iweight = cem_weights3imm]
estimates store imm3
reg britid postattack [iweight = cem_weights3britid]
estimates store britid3
reg englid postattack [iweight = cem_weights3englid]
estimates store englid3	
	

* Results table including Cohen's d estimates
gen days = _n in 1/3
foreach var of varlist sec imm britid englid {
gen n_`var' = .
gen pe_`var' = .
gen se_`var' = .
gen p_`var' = .
gen ll_`var' = .
gen ul_`var' = .
gen sd_`var' = .
gen d_`var' = .
gen dlow_`var' = .
gen dup_`var' = .
forvalues i = 1/3 {
estimates restore `var'`i'
replace n_`var' = `e(N)' if days == `i'
estimates replay `var'`i'
matrix R = r(table)
replace pe_`var' = R[1,1] if days == `i'
replace se_`var' = R[2,1] if days == `i'
replace p_`var' = R[4,1] if days == `i'
replace ll_`var' = R[5,1] if days == `i'
replace ul_`var' = R[6,1] if days == `i'
sum `var' [iweight = cem_weights`i'`var']
replace sd_`var' = `r(sd)' if days == `i'
replace d_`var' = pe_`var'/sd_`var' if days == `i'
replace dlow_`var' = pe_`var'/sd_`var' - 1.96*se_`var'/sd_`var' if days == `i'
replace dup_`var' = pe_`var'/sd_`var' + 1.96*se_`var'/sd_`var' if days == `i'
}
}

drop id - cem_weights3englid
drop _est*
keep in 1/3
gen year = 2019
order year

save "Exact2019.dta", replace





** Figure
*********


* Bring data in correct shape
foreach y in 2017 2019 { 
use "Exact`y'.dta", replace
set obs 15
sum year
replace year = `r(mean)'
gen row = _n
gen outcome = ""
gen pe = .
gen ll = .
gen ul = .
gen n = .
forvalues i = 1/3 {
	replace outcome = "sec" if row == `i' + 3
	sum days if row == `i'
	replace days = `r(mean)' if row == `i' + 3
	sum d_sec if row == `i'
	replace pe = `r(mean)' if row == `i' + 3
	sum dlow_sec if row == `i'
	replace ll = `r(mean)' if row == `i' + 3
	sum dup_sec if row == `i'
	replace ul = `r(mean)' if row == `i' + 3
	sum n_sec if row == `i'
	replace n = `r(mean)' if row == `i' + 3
}
forvalues i = 1/3 {
	replace outcome = "imm" if row == `i' + 6
	sum days if row == `i'
	replace days = `r(mean)' if row == `i' + 6
	sum d_imm if row == `i'
	replace pe = `r(mean)' if row == `i' + 6
	sum dlow_imm if row == `i'
	replace ll = `r(mean)' if row == `i' + 6
	sum dup_imm if row == `i'
	replace ul = `r(mean)' if row == `i' + 6
	sum n_imm if row == `i'
	replace n = `r(mean)' if row == `i' + 6
}
forvalues i = 1/3 {
	replace outcome = "britid" if row == `i' + 9
	sum days if row == `i'
	replace days = `r(mean)' if row == `i' + 9
	sum d_britid if row == `i'
	replace pe = `r(mean)' if row == `i' + 9
	sum dlow_britid if row == `i'
	replace ll = `r(mean)' if row == `i' + 9
	sum dup_britid if row == `i'
	replace ul = `r(mean)' if row == `i' + 9
	sum n_britid if row == `i'
	replace n = `r(mean)' if row == `i' + 9
}
forvalues i = 1/3 {
	replace outcome = "englid" if row == `i' + 12
	sum days if row == `i'
	replace days = `r(mean)' if row == `i' + 12
	sum d_englid if row == `i'
	replace pe = `r(mean)' if row == `i' + 12
	sum dlow_englid if row == `i'
	replace ll = `r(mean)' if row == `i' + 12
	sum dup_englid if row == `i'
	replace ul = `r(mean)' if row == `i' + 12
	sum n_englid if row == `i'
	replace n = `r(mean)' if row == `i' + 12
}
drop in 1/3
drop n_sec - row
save temp`y'.dta, replace
}

* Merge 2017 and 2019
use temp2017.dta, replace
append using temp2019.dta

* Graph preparations
gen ntext = "{it:N} = "
tostring n, replace
replace ntext = ntext + n
gen ypos = .
replace ypos = 0.70 if days == 1
replace ypos = 0.50 if days == 2
replace ypos = 0.30 if days == 3
replace ypos = ypos + 3 if outcome == "sec"
replace ypos = ypos + 2 if outcome == "imm"
replace ypos = ypos + 1 if outcome == "britid"
gen x = 0.30


* Graph
twoway ///
	(scatter ypos pe if days == 1, mcolor(gs12) msymbol(o) msize(medium)) ///
	(rspike ll ul ypos if days == 1, lcolor(gs12) horizontal) ///
	(scatter ypos pe if days == 2, mcolor(gs7) msymbol(s) msize(medium)) ///
	(rspike ll ul ypos if days == 2, lcolor(gs7) horizontal) ///
	(scatter ypos pe if days == 3, mcolor(gs2) msymbol(t) msize(medium)) ///
	(rspike ll ul ypos if days == 3, lcolor(gs2) horizontal) ///
	(scatter ypos x, msymbol(none) mlabel(ntext) mlabsize(vsmall) mlabangle(horizontal) mlabposition(3)) ///
	, ///
	by(year, noixlabel ixtitle graphregion(fcolor(white) lcolor(white)) bgcolor(white) note("{it:Note:} The spikes represent 95% confidence intervals.") legend(pos(6))) ///
	ytitle("") yscale(noline range(0 4)) ///
	ylabel(3.5 "Tough security" 2.5 "Anti-immigration" 1.5 "British identity" 0.5 "English identity", angle(horizontal) nogrid) ///
	xtitle("Effect on political attitudes (Cohen's {it:d})", margin(small))  ///
	xline(0, lwidth(thin) lpattern(solid) lcolor(black) extend) ///
	xlabel(-0.10(.10).40,) xmlabel(-0.10(0.05)0.40, ) xscale(noline) ///
	legend(order(1 3 5) label(1 "± 1 day") label(3 "± 2 days") label(5 "± 3 days") rows(1) size(small) keygap(*1) region(lstyle(none) lcolor(white))) ///
	subtitle(, size(large) align(middle) margin(bottom) nobox fcolor(white))  ///
	graphregion(fcolor(white) ifcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(black)) bgcolor(white) ///
	scheme(s2mono) xsize(6) ysize(3.5)
gr_edit .b1title.draw_view.setstyle, style(no)
gr_edit .plotregion1.xaxis1[1].style.editstyle majorstyle(tickstyle(show_labels(yes))) editcopy
gr_edit .legend.DragBy 0 12
gr_edit .gmetric_mult = 1.15





** Tidy Up
**********

erase "Exact2017.dta"
erase "Exact2019.dta"
erase temp2017.dta
erase temp2019.dta

